<template>
     <div class="container">
        <h3>单位管理</h3>
        <el-form ref="form" label-width="120px" style="margin-top:20px;">
            <el-form-item label="单位：">
                <el-input v-model.trim="kg" style="width:200px;" disabled="disabled"></el-input>
            </el-form-item>
            
            <el-form-item label="单位：">
                <el-input v-model.trim="g" style="width:200px;" disabled="disabled"></el-input>
            </el-form-item>
            <el-form-item v-for="(item,index) in units" :key="index" v-if="index>1"  label="单位：">
                <el-input v-model.trim="item.name" style="width:200px;" :disabled="disabled" @input="accountInput"></el-input>
                <el-button @click = "deleteUnit(index)" v-if="edit" class="delete_btn">删除</el-button>
            </el-form-item>
            <el-form-item>
                <el-button type="primary" @click="unit_save" class="unit_save" v-if="edit">保存</el-button>
                <el-button @click="added_unit" class='added_unit' v-if="edit">添加一条</el-button>
            </el-form-item>
            <el-form-item class="footerFixed" v-if="goback">
                <!-- <el-button type="primary" @click="submitRules('ruleForm')">提交</el-button> -->
                <el-button @click="backPage">返回</el-button>
            </el-form-item>
        </el-form>
    </div>
</template>

<script>
import {unit_findByList,unit_save,unit_delete} from "@/request/api"
export default {
    name: 'unit',
    components: {},
    data() {
        return {
            kg:'kg',
            g:'g',
            disabled:false,
            goback:false,
            type:'',
            units:[
                // {name:'g'},
            ],
            edit:true,
        };
    },
    mounted () {
        this.type = this.$route.query.type
        if(this.type == "added"){
            this.goback = true
        }else{
            this.goback = false
        }
        this.init()
    },
    created(){
            var data = JSON.stringify(this.$store.state.dw)
            
            var a = '编辑'
            if(data.indexOf(a) != -1){
                this.edit = true
                this.disabled = false
            }else{
                this.edit = false
                this.disabled = true
            }
        },
    methods: {
        accountInput(val){
            
        },

        backPage(){
            this.$router.go(-1)
        },
        init(){
            unit_findByList("").then((res)=>{
                this.units = res.data
                if(res.data == ""){
                    let data = [
                        {name:'g'},
                        {name:'kg'}
                    ]
                    unit_save(data).then((res)=>{
                  
                        // this.$notify({
                        //     title: '提示',
                        //     message: '保存成功',
                        //     type: 'success',
                        //     duration:1000
                        // });
                    })
                }
            })
        },
        // 删除 单位
       deleteUnit(index){
           var id = this.units[index].id
               this.$confirm('是否确定删除该单位?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
                }).then(() => {
                    if(id == undefined){
                        this.units.splice(index,1)
                        this.$notify({
                            title: '提示',
                            message: '删除成功',
                            type: 'success',
                            duration:500
                        });
                    }else{
                        unit_delete(id).then((res)=>{
                            this.units.splice(index,1)
                            this.$notify({
                                title: '提示',
                                message: '删除成功',
                                type: 'success',
                                duration:1000
                            });
                        })
                    }
                }).catch(() => {
              
           })
            
       },

        //保存
        unit_save(){
            this.$confirm('是否确定提交?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
                
            }).then(() => {
                for (let l = 0; l < this.units.length; l ++) {
                    for (let j = l+1; j < this.units.length; j++) {
                        if(this.units[l].name == this.units[j].name){
                            var tishi = '单位不能重复'
                            this.$notify({
                                title: '提示',
                                message: tishi,
                                type: 'info',
                                duration:1000
                            });
                            return false
                        } 
                    }
                }
                for (let i = 0; i < this.units.length; i++) {
                    if(this.units[i].name == ""){
                        var tishi = '单位不能为空'
                        this.$notify({
                            title: '提示',
                            message: tishi,
                            type: 'info',
                            duration:1000
                        });
                        return false
                    }
                }
                unit_save(this.units).then((res)=>{
                    this.init()
                    this.$notify({
                        title: '提示',
                        message: '保存成功',
                        type: 'success',
                        duration:500
                    });
                })
                
            }).catch(() => {
                this.$notify({
                    title: '提示',
                    message: '已取消保存',
                    type: 'info',
                    duration:500
                });         
            });
        },
        //添加单位
        added_unit(){
            this.units.push({name:''})
        } 
    },
    
	watch:{
	
	},
};
</script>


<style scoped>
.container{
    min-height:65%;
}
.delete_btn{
    margin-left:6px;
}
</style>
